<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>居中技巧</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        .f{
            width: 300px;
            height: 150px;
            background-color: cadetblue;
            /* margin: auto; */
            
            /* 添加上绝对定位后，破坏了margin:auto效果 */
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -150px;
            margin-top: -75px;
        }

        .x{
            width: 30px;
            height: 30px;
            background-color: lightcoral;
            border-radius: 50px;
            position: absolute;
            top: -5px;
            right:-5px;

            /* 文本居中 */
            text-align: center;
            line-height: 30px;
        }
    </style>
</head>
<body>
    <div class="f">
        <div class="x">X</div>
    </div>
</body>
</html>